package Exercise;

import javax.lang.model.util.ElementScanner6;
import java.util.*;

class pos {
    public double x;
    public double y;

    pos(double x,double y) {
        this.x=x;
        this.y=y;
    }

    public double dist(pos p) {
        double f1=this.x-p.x;
        f1=f1*f1;
        double f2=this.y-p.y;
        f2=f2*f2;
        return Math.sqrt(f1+f2);
    }

    public String toString() {
        return "("+Double.toString(x)+","+Double.toString(y)+")";
    }
}

abstract class GeometricObject implements Comparable<GeometricObject> {
    public abstract double getC();
    public abstract double getS();
    @Override
    public int compareTo(GeometricObject o)
    {
        if(this.getS()<o.getS())
            return -1;
        else if(this.getS()>o.getS())
            return 1;
        else
            return 0;
    }
}

class AreaComparator implements Comparator<GeometricObject> {
    @Override
    public int compare(GeometricObject o1, GeometricObject o2) {
        return o1.compareTo(o2);
    }
}

class Rectangle extends GeometricObject {
    private double dx;
    private double dy;

    private pos p1;
    private pos p2;

    public Rectangle(pos p1, pos p2) {
        this.p1=p1;
        this.p2=p2;
        this.dx=p2.x-p1.x;
        this.dy=p2.y-p1.y;
    }

    public double getC() {
        return dx*2+dy*2;
    }

    public double getS() {
        return dx*dy;
    }

    public String toString() {
        return "Rectangle: "+Double.toString(dx)+","+Double.toString(dy);
    }
}

class Circle extends GeometricObject {
    public pos p;
    public double r;

    public Circle(pos p, double r) {
        this.p=p;
        this.r=r;
    }

    public double getS() {
        return Math.PI*r*r;
    }

    public double getC() {
        return 2*Math.PI*r;
    }

    public String toString() {
        return "Circle: "+p.toString()+","+Double.toString(r);
    }
}

class Triangle extends GeometricObject {
    private double a;
    private double b;
    private double c;

    private pos p1;
    private pos p2;
    private pos p3;

    public Triangle(pos p1, pos p2, pos p3) {
        this.p1=p1;
        this.p2=p2;
        this.p3=p3;
        this.a=p1.dist(p2);
        this.b=p2.dist(p3);
        this.c=p1.dist(p3);
    }

    public double getC() {
        return a+b+c;
    }

    public double getS() {
        double p=a+b+c/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }

    public String toString() {
        return "Triangle: "+Double.toString(a)+","+Double.toString(b)+","+Double.toString(c);
    }
}

class DrawingGraphics {
    static class test {
        public static void testPrint(ArrayList<GeometricObject> allGO) {
            for(GeometricObject r : allGO)
            {
                System.out.println(r);
                System.out.println(r.getC());
                System.out.println(r.getS());
            }
        }

        public static void testSort(ArrayList<GeometricObject> allGO) {
            Collections.sort(allGO);
            test.testPrint(allGO);
        }

        public static void testSortor(ArrayList<GeometricObject> allGO) {
            Collections.sort(allGO, new AreaComparator());
            test.testPrint(allGO);
        }

        public static void doAll(ArrayList<GeometricObject> allGO) {
            test.testPrint(allGO);
            System.out.println("sort1:");
            test.testSort(allGO);
            System.out.println("sort2:");
            test.testSortor(allGO);
        }
    }

    public static void main(String[] args) {
        ArrayList<GeometricObject> allGO=new ArrayList<GeometricObject>();
        allGO.add(new Circle1(new pos(0,0), 1));
        allGO.add(new Rectangle(new pos(1,1), new pos(2,2)));
        allGO.add(new Triangle(new pos(0,0), new pos(1,1), new pos(2,2)));

        System.out.println("1:");
        test.doAll(allGO);

        ArrayList<GeometricObject> allGO2=new ArrayList<GeometricObject>();
        allGO2.add(new Circle1(new pos(0,0), 2));
        allGO2.add(new Rectangle(new pos(-1,-1), new pos(2,2)));
        allGO2.add(new Triangle(new pos(-1,-1), new pos(1,1), new pos(2,2)));

        System.out.println("2:");
        test.doAll(allGO2);
    }
}
